// SINGULARITY HTML5 GAME ENGINE
// -----------------------------
// Copyright(c) 2013 by Codenova.pl

define(['./Core'], function (Core) {
    var Storage = function() {
        "use strict";
        this.Storage = [];
    };

    Storage.prototype = {
        constructor: Storage,

        /**
         * Create new instance
         * @returns {Storage}
         * @constructor
         */
        Create: function() {
            "use strict";
            return new Storage();
        },

        /**
         *
         * @param el
         * @constructor
         */
        Push: function(el) {
            // We have to copy current element to get rid of its reference
            var copy = Object.create(el);
            this.Storage.push(copy);
        },

        /**
         *
         * @param storage
         * @param el
         * @constructor
         */
        StoreElementForUi: function(storage, el) {
            "use strict";
            var _i, _length;

            // Check individual items
            if(typeof el.Hoverable !== "undefined" && el.Hoverable || typeof el.Clickable !== "undefined" && el.Clickable) {
                storage.Push(el);
            }

            // Check group items
            if(typeof el.Inputs !== "undefined" && el.Inputs.length > 0) {
                // We have to override all parameters defined as 'auto'
                // to real values. Not doing this makes engine goes
                // crazy during mouse interaction.
                for(_i = 0, _length = el.Inputs.length - 1; _i <= _length; _i = _i + 1) {
                    el.Inputs[_i].X = el.X + Core.CenterDimension(el.Inputs[_i].X, el.Width, el.Inputs[_i].Width);
                    el.Inputs[_i].Y = el.Y + Core.CenterDimension(el.Inputs[_i].Y, el.Height, el.Inputs[_i].Height);
                    if(typeof el.Inputs[_i].Hover !== 'undefined') {
                        // Override hover values also
                        el.Inputs[_i].Hover.X = el.X + Core.CenterDimension(el.Inputs[_i].Hover.X, el.Width, el.Inputs[_i].Hover.Width);
                        el.Inputs[_i].Hover.Y = el.Y + Core.CenterDimension(el.Inputs[_i].Hover.Y, el.Height, el.Inputs[_i].Hover.Height);
                    }

                    // Store each element individually
                    storage.Push(el.Inputs[_i]);
                }
                storage.Push(el);
            }
        }
    };

    return Storage.prototype.Create;
});